package com.ruicar.afs.cloud.afscase.mq.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.mq.condition.MqMessageQueryCondition;
import com.ruicar.afs.cloud.afscase.mq.entity.CommonFailMqInfo;
import com.ruicar.afs.cloud.afscase.mq.service.CommonFailMqInfoService;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.YesOrNoEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-10-13 22:48
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/mqMessageManagement")
@Api("发送失败mq信息管理")
public class FailedMqInfoManagementController {
    private final CommonFailMqInfoService commonFailMqInfoService;
    /**
     * 多条件查询mq信息列表
     *
     * @param condition
     * @return
     */
    @PostMapping("/getMessagePage")
    @ApiOperation(value = "多条件查询mq信息列表")
    IResponse<IPage<CommonFailMqInfo>> getMessagePage(@RequestBody MqMessageQueryCondition condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        return IResponse.success(commonFailMqInfoService.page(page, Wrappers.<CommonFailMqInfo>query().lambda()
                .like(ObjectUtil.isNotNull(condition.getExchange()),CommonFailMqInfo::getExchange,condition.getExchange())
                .like(ObjectUtil.isNotNull(condition.getCode()),CommonFailMqInfo::getCode,condition.getCode())
                .like(ObjectUtil.isNotNull(condition.getRouterkey()),CommonFailMqInfo::getRouterkey,condition.getRouterkey())
                .eq(ObjectUtil.isNotNull(condition.getStatus()),CommonFailMqInfo::getStatus,condition.getStatus())
                .ge(ObjectUtil.isNotNull(condition.getStartFailedTime()),CommonFailMqInfo::getLastFailedTime,condition.getStartFailedTime())
                .le(ObjectUtil.isNotNull(condition.getEndFailedTime()),CommonFailMqInfo::getLastFailedTime,condition.getEndFailedTime())
                .ge(ObjectUtil.isNotNull(condition.getStartSuccessTime()),CommonFailMqInfo::getSuccessTime,condition.getStartSuccessTime())
                .le(ObjectUtil.isNotNull(condition.getEndSuccessTime()),CommonFailMqInfo::getSuccessTime,condition.getEndSuccessTime())
        ));
    }
    /**
     * mq发送
     * @author ZC.GUO
     * @param
     * @return
     */
    @PostMapping(value = "/sendMq")
    @ApiOperation(value = "mq发送")
    @Transactional(rollbackFor = Exception.class)
    public synchronized IResponse<String> sendMq(@RequestBody MqMessageQueryCondition condition) {
        CommonFailMqInfo commonFailMqInfo = commonFailMqInfoService.getById(condition.getId());
        // TODO: 2020-10-14 zyr 重新发送mq信息
        commonFailMqInfo.setStatus(YesOrNoEnum.yes);
        commonFailMqInfo.setSuccessTime(Calendar.getInstance().getTime());
        commonFailMqInfo.setSuccessOperator(SecurityUtils.getUser().getUserRealName());
        commonFailMqInfoService.updateById(commonFailMqInfo);
        return IResponse.success("发送成功");
    }
}
